package com.alex.controller;

import com.alex.dto.UserDTO;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/final")
public class GetDemoController {
    // GET 请求传参
    // 因为 GET 请求没有 body 所以一般采用 路径参数
    // GET http://localhost:8888/final/1/Alexmaodali
    @GetMapping("/get/{id}/{author}")
    public void getParam(@PathVariable String id,
                         @PathVariable String author) {
        System.out.println(id);// 1
        System.out.println(author);//Alexmaodali
    }

    // 也可以使用 拼接路径参数 Query传参
    // http://localhost:8888/final/get2?id=1&author=Alexmaodali
    @GetMapping("/get2")
    public void getParam2(@RequestParam String id,
                          @RequestParam String author) {
        System.out.println(id);//1
        System.out.println(author);//Alexmaodali
    }

    // 多个Query参数如果一个一个写 很不美观
    // http://localhost:8888/final/get3?id=1&username=username&password=qwer123.&nickname=Alexmaodali
    @GetMapping("/get3")
    public void getParam3(@ModelAttribute UserDTO userDTO) {
        // UserDTO{id='1', username='username', password='qwer123.', nickname='Alexmaodali'}
        System.out.println(userDTO.toString());
    }

    // 结合使用
    // GET http://localhost:8888/final/get4?id=1&username=username&password=qwer123.&nickname=Alexmaodali&plusParam=PlusParamTest
    @GetMapping("/get4")
    public void getParam4(@ModelAttribute UserDTO userDTO, @RequestParam String plusParam) {
        /**
         * UserDTO{id='1', username='username', password='qwer123.', nickname='Alexmaodali'}
         * PlusParamTest
         */
        System.out.println(userDTO.toString());
        System.out.println(plusParam);
    }
}
